/**
 * Created with JetBrains WebStorm.
 * User: Steven
 * Date: 25-4-13
 * Time: 21:15
 * To change this template use File | Settings | File Templates.
 */

/**
 * Attribute class. Description coming soon.
 *
 * @param attributeType
 * @param parent
 * @constructor
 */
function Attribute(attributeType, parent)
{
    this.id = -1; // Unique Attribute ID
    this.baseLevel = 30;
    this.level = 0;
    this.attributeType = attributeType;
    this.attributeTypekey = null;
    this.parent = parent;
    this.governingSkills = {};  // Hashmap for skills
    this.checked = false;       // UI checkbox binding
}

Attribute.prototype.calculateStat = function()
{
    var stat = this.baseLevel + this.level;
    stat += this.parent.race.getAttributeModifier(this.attributeType);
    stat += this.parent.birthsign.getAttributeModifier(this.attributeType);
    stat += this.parent.class.hasFavoredAttribute(this.attributeType) ? 5 : 0;
    return stat;
};

Attribute.prototype.addGoverningSkill = function(skill)
{
    skill.governedAttribute = this;
    this.governingSkills[skill.skillType] = skill;
};

Attribute.prototype.getGoverningSkill = function(skillType)
{
    return skillType in this.governingSkills ? this.governingSkills[skillType] : null;
};

Attribute.prototype.getGoverningSkills = function()
{
    var skills = [];
    var skillKeys = Object.keys(this.governingSkills);
    for(var i = 0; i < skillKeys.length; i++)
    {
        skills.push(this.governingSkills[skillKeys[i]]);
    }
    return skills;
};

Attribute.prototype.getProgress = function()
{
    var skills = this.getGoverningSkills();
    var progress = 0;
    for(var i = 0; i < skills.length; i++)
    {
        progress += skills[i].progressLevel;
    }
    return progress;
};

Attribute.prototype.toString = function()
{
    var returnStr = "";
    returnStr += "AttributeType: " + this.attributeType;
    returnStr += ", BaseLevel: " + this.baseLevel;
    returnStr += ", Level: " + this.level;
    return returnStr;
};

/**
 * Contains the Attributes of a Character.
 *
 * @param parent
 * @constructor
 */

function AttributeSet(parent)
{
    this.parent = parent;
    this.attributes = {};

    // Foreach through all SkillTypes and create the respective Skill objects
    var keys = Object.keys(AttributeTypes);
    for (var i = 0; i < keys.length; i++) {
        // Fetch the current AttributeType
        var currentAttributeType = AttributeTypes[keys[i]];

        // Create the new Attribute
        var attribute = new Attribute(currentAttributeType, this.parent);

        // Configure the attribute ID
        attribute.id = i;
        attribute.attributeTypekey = keys[i];

        // Add the Attribute to the Attribute collection
        this.attributes[currentAttributeType] = attribute;
    }
}

AttributeSet.prototype.getAttribute = function(attributeType)
{
    return this.attributes[attributeType];
}

AttributeSet.prototype.getAttributes = function()
{
    var attributes = [];
    var attrKeys = Object.keys(this.attributes);
    for(var i = 0; i < attrKeys.length; i++)
    {
        attributes.push(this.attributes[attrKeys[i]]);
    }
    return attributes;
};

/**
 * AttributeTypes enumeration
 */
var AttributeTypes =
{
    "strength":"Strength",
    "intelligence":"Intelligence",
    "willpower":"Willpower",
    "agility":"Agility",
    "speed":"Speed",
    "endurance":"Endurance",
    "personality":"Personality",
    "luck":"Luck"
};
Object.freeze(AttributeTypes);